package com.zhuss.core.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.book.StopBookBo;
import com.zhuss.bean.vo.core.book.DetailBookVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.BookDao;
import com.zhuss.core.db.entity.Book;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.AabException;
import com.zhuss.util.JsonUtil;

/**
 * 创建Book processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("bookStop")
public class BookStop extends AbstractProcessor<Object, StopBookBo> implements Processor<Object, StopBookBo> {

    @Autowired
    private BookDao bookDao;
    
    @Autowired
	@Qualifier("bookRead")
	private BookRead bookRead;
    
    /**
     * 校验
     */
    @Override
    public void check(StopBookBo bo) throws AabException {
    }
    
    /**
     * 操作方法
     * @throws AabException 
     */
    @Override
    public Object process(StopBookBo param) throws AabException {
    	
    	Long bookId = param.getBookId();
    	
    	Book record = new Book();
    	record.setBookId(param.getBookId());
    	record.setStatus(param.getStatus());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	bookDao.updateByPrimaryKeySelective(record);
    	
    	String name = ((DetailBookVo) bookRead.detail(bookId)).getName();
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(StopBookBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.Book停用.key());
    	record.setTargetId(param.getBookId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
